package shm;
import java.awt.*;
import javax.swing.*;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
//import java.awt.event.MouseMotionListener;
//import java.awt.event.WindowAdapter;
//import java.awt.event.WindowEvent;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;

import java.awt.BasicStroke;
import java.awt.Color;
//import java.util.*;

//import javax.swing.JButton;
import javax.swing.JTextField;


public class Topology implements ActionListener{

	
	public static boolean canRepaint = true;
	// topology frame MainUI
    public static JFrame topo;
    // left black panel on topology frame
    public static myPanel mp;

    public static JTextField jtf;

    // used for drag/drop indicator
    public static int X0=-1;
    public static int Y0=-1;
    public static int dragIndex = -1;	// the node being dragged.
    

    public static boolean check = false;

    public static JTextArea jta = null;
    public static JPanel mp2 = null;

	/**
	 * Create the frame.
	 */
	public Topology()
	{
		initalize();
	}
	public static void initalize() {

		NodeInformation.init();
		

		topo = new JFrame();
		topo.setTitle("Topology");
		topo.setSize(800,500);
		topo.setLayout(null);
		
		mp = new myPanel();
		mp.setBackground(Color.black);
		mp.setBounds(0,0,500,500);
		
		jta = new JTextArea();
		jta.setLineWrap(true);
		// put the textarea into scroll panel to show the scroll bar
	    JScrollPane jsp = new JScrollPane(jta);
		jsp.setBounds(501, 0, 289, 500);

		topo.add(mp);
		topo.add(jsp);
		topo.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				topo.setVisible(false); 
				}
		    });
	}

	public void actionPerformed(ActionEvent e) {
		
	}
	
	public static void analyze(int id, int parentId, int ext, int quality) throws IOException
	{
		if(id > 200 || id <= 0)
			return;
		
		NodeInformation old = NodeInformation.getNode(id);
		
		if( old == null ) // does not exist
		{
			int x = (id / 5 + 1) * 80;
			int y = (id % 5 + 1) * 80;
			// Create it
			old = NodeInformation.creatNode(id, parentId, x, y, ext, quality);
			
			String temp ="New node ID: "+id+" Parent: "+parentId+" Ext: "+ext+" Quality: "+quality;
			Topology.jta.append(temp+"\r\n");
		}
		else 
		{
    	 	if( NodeInformation.getParent(id) != parentId)
            {
	          String temp = "Change route ID: "+id+" Parent: "+parentId+" Ext: "+ext+" Quality: "+quality;
	          Topology.jta.append(temp+"\r\n");
            }
    	 	
    	 	// update value
    	 	old.Ext = ext;
    	 	old.Parent = parentId;
    	 	old.LinkQuality = quality;
		}
		
		if(Topology.canRepaint==true)
		{
			Topology.mp.repaint();
		}
	}
	

}

class myPanel extends JPanel implements MouseMotionListener,MouseListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6382186957990950614L;
	public myPanel()
	{
		addMouseListener(this);
		addMouseMotionListener(this);
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		
		for(int i=0; i < NodeInformation.getSize(); i++)
		{
			NodeInformation ni = NodeInformation.getNodeAt(i);
			int x = ni.X;
			int y = ni.Y;
			
			g.setColor(Color.black);
			g.drawOval(x-6, y-6, 12, 12);
			
			if(ni.state==false&&ni.Id!=0)
			{
				g.setColor(Color.gray);
			}
			else{
				g.setColor(Color.red);
			}
			g.fillOval(x-6, y-6, 12, 12);
			
			g.drawString(ni.Id+" "+ni.Ext+" "+ni.LinkQuality, x-13, y-11);
			
				
			NodeInformation nip = NodeInformation.getNode(ni.Parent);
				
			if( nip != null )//Father has been established
			{
				int x1 = nip.X;
				int y1 = nip.Y;
				g.setColor(Color.green);
				Graphics2D g2 = (Graphics2D)g;     //g��Graphics���� 
				g2.setStroke(new   BasicStroke(3.0f));
                g2.drawLine(x, y, x1, y1);				
			} // end if
		} // end for
	}
	
	public void mouseClicked(MouseEvent e) {
		//System.out.println("Clicked");
	}
	public void mouseEntered(MouseEvent e) {
			}
	public void mouseExited(MouseEvent e) {
	}
	
	public void mousePressed(MouseEvent e) {
		
		int x = e.getPoint().x;
//		int y = e.getPoint().y;
		
		for(int i = 0; i < NodeInformation.getSize(); i++)
		{
			NodeInformation ni = NodeInformation.getNodeAt(i);
			int nix = ni.X;
			int niy = ni.Y;
			if(Math.abs(nix - x) <= 6 && Math.abs(niy-ni.Y) <= 6)
			{
				Topology.X0=nix;
				Topology.Y0=niy;
				Topology.dragIndex = i;
				break;
			}
		
		}
	}
	public void mouseReleased(MouseEvent e) {
		Topology.dragIndex=-1;
		Topology.canRepaint=true;
		System.out.println("Released");
		
	}
	public void mouseDragged(MouseEvent e) {
		if(Topology.dragIndex!=-1)
		{
			Topology.canRepaint=false;
			int x = e.getPoint().x;
			int y = e.getPoint().y;
			if(x>598||x<2||y>598||y<2)
			{
				return;
			}
			NodeInformation ni = NodeInformation.getNodeAt(Topology.dragIndex);
			ni.X=x;
			ni.Y=y;
			repaint();
		}
	}
	public void mouseMoved(MouseEvent e) {	
	}

}
